package io.gitee.cloudladder.obsidian.swagger2.example.controller;

import io.gitee.cloudladder.obsidian.swagger2.annotation.*;
import io.gitee.cloudladder.obsidian.swagger2.example.entity.User;
import io.gitee.cloudladder.obsidian.swagger2.example.entity.Response;
import io.gitee.cloudladder.obsidian.swagger2.example.generator.CreateUserResponseExampleGenerator;
import io.gitee.cloudladder.obsidian.swagger2.example.generator.GetUserResponseExampleGenerator;
import io.gitee.cloudladder.obsidian.swagger2.example.generator.UpdateUserResponseExampleGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Api(value = "Test Document Controller")
public class TestDocumentController {

    @GetMapping("/user/{id}")
    @ApiOperationSort(1)
    @ApiGuideImage("/image/user/001.png")
    @ApiOperation(value = "get user by id", notes = "notes for get user")
    @ApiResponseExample(GetUserResponseExampleGenerator.class)
    @ApiErrorCodes({
            @ApiErrorCode(code = 1000001, message = "user id is invalid")
    })
    public Response<User> getUser(@PathVariable String id) {
        log.info("get user by id: id = {}", id);
        return new GetUserResponseExampleGenerator().generate();
    }

    @PostMapping("/user")
    @ApiOperationSort(2)
    @ApiOperation(value = "create a user object", notes = "notes for create user")
    @ApiResponseExample(CreateUserResponseExampleGenerator.class)
    @ApiErrorCodes({
            @ApiErrorCode(code = 1000002, message = "age is too max!"),
            @ApiErrorCode(code = 1000003, message = "sex is matched a wrong value!")
    })
    public Response<String> createUser(@RequestBody User user) {
        log.info("create user, user = {}", user);
        return new CreateUserResponseExampleGenerator().generate();
    }

    @PutMapping("/user/{id}")
    @ApiOperationSort(3)
    @ApiOperation(value = "update user object", notes = "notes for update user")
    @ApiResponseExample(UpdateUserResponseExampleGenerator.class)
    @ApiErrorCodes({
            @ApiErrorCode(code = 1000001, message = "user id is invalid"),
            @ApiErrorCode(code = 1000002, message = "error code 1000004"),
            @ApiErrorCode(code = 1000003, message = "sex is matched a wrong value!")
    })
    public Response<Boolean> updateUser(@PathVariable String id, @RequestBody User user) {
        log.info("update user by id, id = {}, user = {}", id, user);
        return new UpdateUserResponseExampleGenerator().generate();
    }

    @DeleteMapping("/user/{id}")
    @ApiOperationSort(4)
    @ApiOperation(value = "delete user by id", notes = "notes for delete user")
    @ApiErrorCodes({
            @ApiErrorCode(code = 1000001, message = "user id is invalid")
    })
    public Response<Boolean> deleteUser(@PathVariable String id) {
        log.info("delete user by id, id = {}", id);
        return new UpdateUserResponseExampleGenerator().generate();
    }

}

